package com.whzm.mapper;

import com.whzm.pojo.Resources;
import com.whzm.pojo.vo.FlowMatchVo;
import com.whzm.pojo.vo.ResourcesListVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface ResourcesMapper {
    /**
     * 根据userId获取符合条件的资源列表
     * @param userId
     * @return
     */
    @Select("select id,type,name,user_id,status,is_deleted from resources where user_id = #{userId} and is_deleted = 0 and status = 0")
    List<Resources> getResourcesByUserId(String userId);

    /**
     * 根据当前资源类型，去数据库中获取落地方式符合的其他资源id
     * @param resourcesId
     * @return
     */
    @Select("select distinct resources_id  from resources_implementation where implementation_id in (select implementation_id from resources_implementation where resources_id = #{resourcesId} ) and resources_id != #{resourcesId}")
    List<String> getResourcesByImplementation(String resourcesId);

    /**
     * 根据当前资源类型，去数据库中获取合作方式符合的其他资源
     * @param resourcesId
     * @return
     */
    @Select("select distinct resources_id from resources_cooperation where cooperation_id in( select cooperation_id from resources_cooperation where resources_id = #{resourcesId} ) and resources_id != #{resourcesId}")
    List<String> getResourcesByCooperation(String resourcesId);

    /**
     * 得到推荐资源
     * @param params
     * @return
     */
    List<ResourcesListVo> getRecommendationResources(Map<String,Object> params);

    /**
     * 得到已发布的资源
     * @param userId
     * @return
     */
    @Select("select * from resources where user_id = #{userId} and is_deleted = 0")
    List<Resources> getPublishedResources(String userId);

    /**
     * 根据用户id，获取待匹配资源
     * @param userId
     * @return
     */
    @Select("select * from flow_match where (advertiser_id = #{userId} or flower_id = #{userId}) and match_status = 0 and is_deleted = 0")
    List<FlowMatchVo> getToBeMatchResources(String userId);

    /**
     * 根据resourcesId获取返回前端的中间类信息
     * @param resourcesId
     * @return
     */
    ResourcesListVo getResourcesVoByRId(String resourcesId);

    /**
     * 当同意或者拒绝匹配时，修改匹配状态
     * map中的参数是matchStatus,matchId
     * @param params
     * @return
     */
    @Update("update flow_match set match_status = #{matchStatus} where id = #{matchId}")
    Integer updateFlowMatch(Map<String,Object> params);

    /**
     * 当同意匹配后，修改资源(resources)的匹配状态为1
     * @param resourcesId
     * @return
     */
    @Update("update resources set status = 1 where id = #{resourcesId}")
    Integer updateResourceStatus(String resourcesId);

    /**
     * 根据resourcesId获取一条资源信息
     * @param resourcesId
     * @return
     */
    @Select("select *from resources where id = #{resourcesId}")
    Resources getResourcesByRId(String resourcesId);

    /**
     * 添加一条匹配数据
     * @param flowMatchVo
     * @return
     */
    @Insert("Insert into flow_match(id,flow_id,advertise_id,match_status,advertiser_id,flower_id) values(#{id},#{flowId},#{advertiseId},#{matchStatus},#{advertiserId},#{flowerId})")
    Integer addFlowMatchInfo(FlowMatchVo flowMatchVo);


    /*********************************hq代码********************************************/
    @Select("select * from resources where name like #{resourceName}")
    Resources queryByResName(String resourceName);

    /**
     * 首页置顶
     */
    List<ResourcesListVo> queryTop();
}
